1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package groovy.swing;
20
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import javax.swing.table.AbstractTableModel;
25
26 /**
27 * A sample table model
28 *
29 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
30 */
31 public class MyTableModel extends AbstractTableModel {
32
33 private static final Logger log = Logger.getLogger(MyTableModel.class.getName());
34
35 public MyTableModel() {
36 }
37
38 final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
39 final Object[][] data = { { "Mary", "Campione", "Snowboarding", Integer.valueOf(5), new Boolean(false)}, {
40 "Alison", "Huml", "Rowing", Integer.valueOf(3), new Boolean(true)
41 }, {
42 "Kathy", "Walrath", "Chasing toddlers", Integer.valueOf(2), new Boolean(false)
43 }, {
44 "Mark", "Andrews", "Speed reading", Integer.valueOf(20), new Boolean(true)
45 }, {
46 "Angela", "Lih", "Teaching high school", Integer.valueOf(4), new Boolean(false)
47 }
48 };
49
50 public int getColumnCount() {
51 return columnNames.length;
52 }
53
54 public int getRowCount() {
55 return data.length;
56 }
57
58 public String getColumnName(int col) {
59 return columnNames[col];
60 }
61
62 public Object getValueAt(int row, int col) {
63 return data[row][col];
64 }
65
66 /*
67 * JTable uses this method to determine the default renderer/
68 * editor for each cell. If we didn't implement this method,
69 * then the last column would contain text ("true"/"false"),
70 * rather than a check box.
71 */
72 public Class getColumnClass(int c) {
73 return getValueAt(0, c).getClass();
74 }
75
76 /*
77 * Don't need to implement this method unless your table's
78 * editable.
79 */
80 public boolean isCellEditable(int row, int col) {
81 //Note that the data/cell address is constant,
82 //no matter where the cell appears onscreen.
83 if (col < 2) {
84 return false;
85 }
86 else {
87 return true;
88 }
89 }
90
91 /*
92 * Don't need to implement this method unless your table's
93 * data can change.
94 */
95 public void setValueAt(Object value, int row, int col) {
96 if (log.isLoggable(Level.FINE)) {
97 log.fine(
98 "Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")");
99 }
100
101 if (data[0][col] instanceof Integer && !(value instanceof Integer)) {
102 //With JFC/Swing 1.1 and JDK 1.2, we need to create
103 //an Integer from the value; otherwise, the column
104 //switches to contain Strings. Starting with v 1.3,
105 //the table automatically converts value to an Integer,
106 //so you only need the code in the 'else' part of this
107 //'if' block.
108 //XXX: See TableEditDemo.java for a better solution!!!
109 try {
110 data[row][col] = Integer.valueOf(value.toString());
111 fireTableCellUpdated(row, col);
112 }
113 catch (NumberFormatException e) {
114 log.log(Level.SEVERE, "The \"" + getColumnName(col) + "\" column accepts only integer values.");
115 }
116 }
117 else {
118 data[row][col] = value;
119 fireTableCellUpdated(row, col);
120 }
121 }
122
123 }